//
//  customCollectionViewCell.m
//  DesighBook
//
//  Created by 梅明 on 15/9/21.
//  Copyright (c) 2015年 梅明. All rights reserved.
//

#import "customCollectionViewCell.h"
#import "HttpRequestManager.h"
#import "Collectiondetailmodel.h"
#import "Askdetailmodel.h"
#import "UIImageView+AFNetworking.h"
#import "AskButtonView.h"
#import "SecscrollTableViewCell.h"
#import "secscrollTableViewCelltwo.h"
#define viewwidth self.view.frame.size.width
#define ImageHight 300.0f

@implementation customCollectionViewCell 


- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _askArray = [[NSMutableArray alloc]init];
        _collectionArray = [[NSMutableArray alloc]init];
        _page = 0;
    }
    return self;
}

-(void)createTableviewWithId:(NSString *)numberid
{
    _numberid = numberid;
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
    [self loadDataFromNetwithId:numberid];
    [_mytableView registerNib:[UINib nibWithNibName:@"SecscrollTableViewCell" bundle:nil] forCellReuseIdentifier:@"cellone"];
    [_mytableView registerNib:[UINib nibWithNibName:@"secscrollTableViewCelltwo" bundle:nil] forCellReuseIdentifier:@"celltwo"];
}



-(void)createZoompic
{
    
    //设置contentInset属性（上左下右 的值）
    _mytableView.contentInset = UIEdgeInsetsMake(ImageHight, 0, 0, 0);
    _mytableView.contentOffset = CGPointMake(0, -ImageHight);

    _mytableView.tableFooterView = [[UIView alloc]init];
   
    _zoomImageView = [[UIImageView alloc]init];
    _zoomImageView.image = nil;
    _zoomImageView.clipsToBounds = YES;
    [_zoomImageView setImageWithURL:[NSURL URLWithString:_imgUrl]];
    _zoomImageView.frame = CGRectMake(0, -ImageHight, self.frame.size.width, ImageHight);
    

    _zoomImageView.contentMode = UIViewContentModeScaleAspectFill;//重点（不设置那将只会被纵向拉伸）

    
    //将_zoomImageView添加到tableview上
    [_mytableView addSubview:_zoomImageView];
    
    //设置autoresizesSubviews让子类自动布局
    _zoomImageView.autoresizesSubviews = YES;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;//根据实际选择加不加上NavigationBarHight（44、64 或者没有导航条）
    
        CGRect frame = _zoomImageView.frame;
        frame.origin.y = y;
        frame.size.height =  -y;//contentMode = UIViewContentModeScaleAspectFill时，高度改变宽度也跟着改变
        _zoomImageView.frame = frame;
    
    

}

-(void)loadDataFromNetwithId:(NSString *)numberid
{
    [_askArray removeAllObjects];
    [_collectionArray removeAllObjects];
    [[HttpRequestManager shareInstnce]getInfoWithNumberId:numberid AndSuccess:^(id responseObjec) {
        NSDictionary *dic = responseObjec;

        _imgUrl = dic[@"imgUrl"];
        [self createZoompic];
        _shareUrl = dic[@"shareUrl"];
        NSArray *arr1 = dic[@"askList"];
        
        for (NSDictionary *dic1 in arr1) {
            Askdetailmodel *model = [[Askdetailmodel alloc]init];
            model.createTime = dic1[@"createTime"];
            model.describe = dic1[@"desc"];
            model.facepic = dic1[@"facePic"];
            model.hits = dic1[@"hits"];
            model.imgurl = dic1[@"imgUrl"];
            model.title = dic1[@"title"];
            model.nick = dic1[@"nick"];
            [_askArray addObject:model];
        }
       
        
        NSArray *arr2 = dic[@"colList"];
        
        for (NSDictionary *dic2 in arr2) {
            Collectiondetailmodel *model = [[Collectiondetailmodel alloc]init];
            model.createTime = dic2[@"createTime"];
            model.facepic = dic2[@"facePic"];
            model.name = dic2[@"name"];
            model.nick = dic2[@"nick"];
            [_collectionArray addObject:model];
        }
        
        
        [_mytableView reloadData];
        
    } AndFailure:^(NSError *error) {
        

    }];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_page == 0) {
        return _askArray.count;
    }
    else
    {
        return _collectionArray.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(_page == 0)
    {
        Askdetailmodel *model = _askArray[indexPath.row];
        SecscrollTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellone"];
        [cell.faceimg setImageWithURL:[NSURL URLWithString:model.facepic]];
        cell.faceimg.clipsToBounds = YES;
        cell.faceimg.layer.cornerRadius = 24;
        cell.labelone.text = [NSString stringWithFormat:@"%@提出了一个问题",model.nick];
        cell.labelTwo.text = model.title;
        return cell;
    }else
    {
        Collectiondetailmodel *model = _collectionArray[indexPath.row];
        secscrollTableViewCelltwo *cell = [tableView dequeueReusableCellWithIdentifier:@"celltwo"];
        [cell.facepic setImageWithURL:[NSURL URLWithString:model.facepic]];
        cell.facepic.clipsToBounds = YES;
        cell.facepic.layer.cornerRadius = 24;
        cell.labelone.text = [NSString stringWithFormat:@"%@收藏图片到%@",model.nick,model.name];
        cell.labeltwo.text = model.createTime;
        return cell;
    }
    
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    _view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 100)];
    
    //对图片提问button
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, self.frame.size.width-20, 40)];
    [button setTitle:@"对图片提问" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor redColor]];
    [button setImage:[UIImage imageNamed:@"ico_signin.png"] forState:UIControlStateNormal];
    button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 50);
    [_view addSubview:button];
    

    NSArray *arr = @[@"问答",@"收藏"];
    [_view addSubview:_view1];
    //问答和收藏两个button
    for (int i = 0; i<arr.count; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(_view.frame.size.width/2*i, 60, _view.frame.size.width/2, 40)];
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [button setBackgroundColor:[UIColor whiteColor]];
        button.tag = 100 + i;
        if (i == 0) {
            button.selected = YES;
            _page = 0;
        }
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [_view addSubview:button];
    }

    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-1, _view.frame.size.width, 1)];
    view2.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _downview = [[UIView alloc]initWithFrame:CGRectMake(0, 98, _view.frame.size.width/2, 2)];
    _downview.backgroundColor = [UIColor redColor];
    [_view addSubview:view2];
    [_view addSubview:_downview];
    self.backgroundColor = [UIColor whiteColor];
    
    
    return _view;
}

//两个button的点击事件
-(void)click:(UIButton *)button
{
  
    
    for (UIButton *but in self.subviews) {
        if ([but isKindOfClass:[UIButton class]]) {
            but.selected = NO;
        }
    }
    button.selected = YES;

    [UIView animateWithDuration:0.2 animations:^{
        
        CGRect frame = _downview.frame;
        if (_downview.frame.origin.x/(_view.frame.size.width/2)>(button.tag-100)) {
            frame.origin.x = (button.tag-100)*-10;
            _downview.frame = frame;
        }else {
            frame.origin.x = (button.tag-100)*_view.frame.size.width/2+10;
            _downview.frame = frame;
        }
        
    } completion:^(BOOL finished) {
        CGRect frame = _downview.frame;
        frame.origin.x =(button.tag-100)*_view.frame.size.width/2;
        _downview.frame = frame;
        
    }];
    
    if(button.tag==100)
    {
        _page = 0;
    }
    else
    {
        _page =1;
    }
    [_mytableView reloadData];
    
}


- (void)askButtonView:(UIView *)askButtonView flag:(NSInteger)flag
{
    if (flag == 0) {
        _page = 0;
    }
    else
    {
        _page = 1;
    }
    [self loadDataFromNetwithId:_numberid];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 100;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

@end
